package com.zzk.lanouhn.myapplication1.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;

import com.baoyz.widget.PullRefreshLayout;
import com.google.gson.Gson;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.activity.HotDetailsActivity;
import com.zzk.lanouhn.myapplication1.activity.KindsDetailsActivity;
import com.zzk.lanouhn.myapplication1.adapters.MyGridView1Adapter;
import com.zzk.lanouhn.myapplication1.adapters.MyGridView2Adapter;
import com.zzk.lanouhn.myapplication1.modles.HotMyGridView;
import com.zzk.lanouhn.myapplication1.modles.KindsMyGridView;
import com.zzk.lanouhn.myapplication1.utils.HttpUtils;
import com.zzk.lanouhn.myapplication1.view.MyGridView;

import java.util.List;

/**
 * Created by lanouhn on 16/9/1.
 */
public class MessageShowFragment extends Fragment{

    private MyGridView hotMyGridView,kindsMyGridView;
    private List<HotMyGridView.DataBean> hot;
    private KindsMyGridView kinds;
    private MyGridView1Adapter gridView1Adapter;
    private MyGridView2Adapter gridView2Adapter;
    private PullRefreshLayout layout;
    private int page=0;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_show_message,null);
        hotMyGridView= (MyGridView) view.findViewById(R.id.gv_hot_show_message);
        kindsMyGridView= (MyGridView) view.findViewById(R.id.gv_kinds_show_message);
        layout= (PullRefreshLayout) view.findViewById(R.id.refreshLayout);
        getDataFromWeb();
        initEvent();
        refresh();
        return view;
    }

    private void refresh(){
        layout.setRefreshStyle(PullRefreshLayout.STYLE_RING);
        layout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page=0;
                getDataFromWeb();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        layout.setRefreshing(false);//停止刷新
                    }
                }, 2000);
            }
        });
    }
    private void getDataFromWeb() {
        //分类
        String kindsUrl="http://open4.bantangapp.com/community/post/index?";
        String kindsDataUrl="app_id=com.jzyd.BanTang&client_id=bt_app_android" +
                "&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d" +
                "&track_user_id=&oauth_token=&track_deviceid=863121028526879" +
                "&track_device_info=MI+2S&channel_name=xiaomi" +
                "&app_installtime=1473128006&app_versions=5.8.7" +
                "&os_versions=5.0.2&screensize=720&v=19";
        try {
            HttpUtils.doPostAsyn(kindsUrl, kindsDataUrl, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson kindGson=new Gson();
                    kinds=kindGson.fromJson(result,KindsMyGridView.class);
                    if (kinds.getData().getCategory_list().size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        //热门推荐


        String hotUrl="http://open4.bantangapp.com/community/post/hotRecommend?";
        String hotDataUrl="app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=&oauth_token=&track_deviceid=863121028526879&track_device_info=MI+2S&channel_name=xiaomi&app_installtime=1473128006&app_versions=5.8.7&os_versions=5.0.2&screensize=720&v=19&page=0&pagesize=18";
        try {
            HttpUtils.doPostAsyn(hotUrl, hotDataUrl, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson hotGson=new Gson();
                    hot=hotGson.fromJson(result,HotMyGridView.class).getData();
                    if (hot.size() > 0) {
                        handler.sendEmptyMessage(2);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch(msg.what){
                case 0:
                    Toast.makeText(getActivity(), "没数据11111", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    gridView1Adapter=new MyGridView1Adapter(getActivity(),kinds);
                    kindsMyGridView.setAdapter(gridView1Adapter);
                    break;
                case 2:
                    gridView2Adapter=new MyGridView2Adapter(getActivity(),hot);
                    hotMyGridView.setAdapter(gridView2Adapter);
                    break;
              default:

                  break;
            }
        }
    };

    private void initEvent(){
        kindsMyGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent1=new Intent(getActivity(),KindsDetailsActivity.class);
                Bundle bundle=new Bundle();
                String itemid=kinds.getData().getCategory_list().get(position).getId();
                bundle.putString("id",itemid);
                intent1.putExtras(bundle);
                startActivity(intent1);
            }
        });
        hotMyGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent2=new Intent(getActivity(), HotDetailsActivity.class);
                Bundle bundle=new Bundle();
                bundle.putInt("position",position);
                intent2.putExtras(bundle);
                startActivity(intent2);
            }
        });
        
    }
}
